#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_radio.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manages the radio pop-up window

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The Filters pop-up.
class RadioGui:
    
    #---------------------------------------------------------------------
    def __init__(self, radio_tree, caps, dsp_pub, persist_service, callback):
        self.radio_tree = radio_tree
        self.caps = caps
        self.dsp_pub = dsp_pub
        self.persist_service = persist_service
        self.callback = callback
        self.ignore = False
        
        #-----------------------------------------------------------------------
        # Wire up the events
        dic =   {
            # Radios
            "on_tb_radio_default_toggled" : self.default_radio,
            "on_tb_radio_1_toggled" : self.radio_1,
            "on_tb_radio_2_toggled" : self.radio_2,
            "on_tb_radio_3_toggled" : self.radio_3,
        }
        
        # This will connect up all the events in one hit
        self.radio_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        self.bdefault = self.radio_tree.get_widget('tb_radio_default')
        self.br1 = self.radio_tree.get_widget('tb_radio_1')
        self.br2 = self.radio_tree.get_widget('tb_radio_2')
        self.br3 = self.radio_tree.get_widget('tb_radio_3')
        
        # Avoid circular events
        self.ignore = False
        
        self.w=self.radio_tree.get_widget('Radios')
        self.w.set_position(gtk.WIN_POS_MOUSE)

        # Widget map
        self.dict = {'DEFAULT':0,'RADIO_1':1,'RADIO_2':2,'RADIO_3':3}
        self.widgets = [self.bdefault, self.br1, self.br2, self.br3]        
        
        # There are no capabilities as either there are multi-rx or not and 4 are allowed.

    #--------------------------------------------------------------------- 
    # Public Interface
    #---------------------------------------------------------------------
    def show_window(self):
        self.w.show_all()
    
    #---------------------------------------------------------------------
    def hide_window(self):
        self.w.hide()
        
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
    # Events for the radio buttons
    #---------------------------------------------------------------------  
    #---------------------------------------------------------------------  
    def default_radio(self, widget):
        if not self.ignore:
            self.callback(0)
            self.ignore = True
            self.inactive()
            self.bdefault.set_active(True)
            self.ignore = False
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def radio_1(self, widget):
        if not self.ignore:
            self.callback(1)
            self.ignore = True
            self.inactive()
            self.br1.set_active(True)
            self.ignore = False
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def radio_2(self, widget):
        if not self.ignore:
            self.callback(2)
            self.ignore = True
            self.inactive()
            self.br2.set_active(True)
            self.ignore = False
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def radio_3(self, widget):
        if not self.ignore:
            self.callback(3)
            self.ignore = True
            self.inactive()
            self.br3.set_active(True)
            self.ignore = False
            self.w.hide()
     
    #---------------------------------------------------------------------   
    def inactive(self):
        self.bdefault.set_active(False)
        self.br1.set_active(False)
        self.br2.set_active(False)
        self.br3.set_active(False)